package com.example.reflection.config.util;

import com.example.reflection.config.WriteFile;

import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class ReflexUtil {
    private static StringBuffer sBuffer;

    public static void getJar(String jar) throws Exception {
        try {
            File file = new File(jar);
            URL url = file.toURI().toURL();
            URLClassLoader classLoader = new URLClassLoader(new URL[]{url},
                    Thread.currentThread().getContextClassLoader());

            JarFile jarFile = new JarFile(jar);
            Enumeration<JarEntry> enumeration = jarFile.entries();
            JarEntry jarEntry;

            sBuffer = new StringBuffer();    //存数据

            while (enumeration.hasMoreElements()) {
                jarEntry = enumeration.nextElement();

                if (!jarEntry.getName().contains("META-INF")) {
                    String classFullName = jarEntry.getName();
                    if (!classFullName.contains(".class")) {
                        classFullName = classFullName.substring(0, classFullName.length() - 1);
                    } else {
                        // 去除后缀.class，获得类名
                        String className = classFullName.substring(0, classFullName.length() - 6).replace("/", ".");
                        Class<?> myClass = classLoader.loadClass(className);
                        sBuffer.append("类名\t\t：").append(className).append("\n");
                        System.out.println("类名\t\t：" + className);

                        // 获得修饰符、属性类型、属性名

                        Field[] fields = className.getClass().getDeclaredFields();
                        for (Field field : fields) {
//                            sBuffer.append("修饰符\t：").append(Modifier.toString(field.getModifiers())).append("\n");
//                            System.out.println("修饰符\t：" + Modifier.toString(field.getModifiers()));
//
//                            sBuffer.append("属性类型\t：").append(field.getType().getName()).append("\n");
//                            System.out.println("属性类型\t：" + field.getType().getName());

                            sBuffer.append("属性名\t：").append(field.getName()).append("\n");
                            System.out.println("属性名\t：" + field.getName());
                        }

                        // 获得方法名
                        Method[] methods = myClass.getMethods();
                        for (Method method : methods) {
                            if (method.toString().indexOf(className) > 0) {
                                String substring = method.toString().substring(method.toString().indexOf(className));
                                sBuffer.append("方法名\t：").append(substring).append("\n");
                                System.out.println("方法名\t：" + substring);
                            }
                        }
                        sBuffer.append("--------------------------------------------------------------------------------").append("\n");
                        System.out.println("--------------------------------------------------------------------------------");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sBuffer.append("End");
            System.out.println("End");

            WriteFile.write(sBuffer);    //写文件
        }
    }
}
